//获取应用实例
const app = getApp();

Page({
  data: {
    nums: [
      { num: 1 }
    ],
    num: 1,
    title: '',
    content: '',
    actualPice: 0,//实际支付金额
    couponPrice: 0,//优惠券金额
    couponId: '',//优惠券id
    tihuoWay: 'A4文档打印',
    printType: 1,
    facilityCode: '',
    paperNumber: 0,
    fanpage: 1,
    pice: 10,
    type: 1,
    chicun: '',//尺寸信息 
    all_sum: 1,
    choice_star: 1,
    choice_end: 1,
    dycolor: '',//打印颜色
    all_pice: 10,
    isColours: false,
    payType: 1,
    typeCode: 11,
    payid: '',
    inputPassword: '', //输入的密码
    passwordInputHidden: true, //hidden是true 默认隐藏
    rmb: 10,
    couponTitle: '请选择优惠券',
    select_f: [{
      name: '1',
      value: '微信支付',
      checked: 'true'
    }, {
      name: '0',
      value: '余额支付',
    },

    ],
    select_color: [{
      name: '0',
      value: '彩色',
      checked: 'true'
    }, {
      name: '1',
      value: '黑白',
    },

    ],
    // 组件所需的参数
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '提交订单', //导航栏 中间的标题
    },
    // 此页面 页面内容距最顶部的距离
    height: app.globalData.height * 2 + 20,
  },
  onLoad(options) {
    console.log(options);
    let that = this;
    let content = options.content;
    let chicun = options.chicun;
    let title = options.title;
    let typeCode = options.id;
    let facilityCode = app.globalData.jqm;
    that.setData({
      content: content,
      facilityCode: facilityCode
    })
    let url = app.globalData.URL + 'facility/print/price/facility/price'
    let data = {
      count: 1,
      isColours: true,
      isDoubleFaced: false,
      printfile: content,
      type: typeCode,
      facilityCode: facilityCode
    }
    wx.showLoading({
      title: '加载中…',
    })
    wx.request({
      url: url,
      method: 'GET',
      header: {
        // 'content-type': 'application/json',
        'Authorization': getApp().globalData.token // 默认值
      },
      data: data,
      success: function (res) {
        console.log(res);
        if (res.statusCode !== 200 && res.data.code !== 200) {
          wx.showToast({
            title: '价格获取失败',
            icon: 'none'
          })
          return false;
        }
        that.setData({
          typeCode: typeCode,
          title: title,
          type: 1,
          isColours: true,
          // pice:res.data.data.price,
          chicun: chicun,
          paperNumber: res.data.data.paperNumber,
          content: content,
          typeCode: typeCode,
          pice: res.data.data.price,
          all_pice: res.data.data.price
        })
        console.log(that.data.pice)
      },
      complete: function () {
        wx.hideLoading();
      }
    })
  },
  select_zhifu: function (e) {
    let pay_type = e.detail.value;
    this.setData({
      payType: pay_type
    })
  },
  select_yanse: function (e) {
    let dycolor = e.detail.value;
    console.log(dycolor);
    this.setData({
      dycolor: dycolor,
      isColours:  dycolor == 0
    })
    var that = this;
    let url = app.globalData.URL + 'facility/print/price/facility/price'
    let data = {
      count: 1,
      isColours: dycolor == 0,
      isDoubleFaced: false,
      printfile: this.data.content,
      type: this.data.typeCode,
      facilityCode: this.data.facilityCode
    }
    wx.showLoading({
      title: '加载中…',
    })
    wx.request({
      url: url,
      method: 'GET',
      header: {
        // 'content-type': 'application/json',
        'Authorization': getApp().globalData.token // 默认值
      },
      data: data,
      success: function (res) {
        if (res.statusCode !== 200 && res.data.code !== 200) {
          wx.showToast({
            title: '价格获取失败',
            icon: 'none'
          })
          return false;
        }
        that.setData({
          paperNumber: res.data.data.paperNumber,
          pice: res.data.data.price,
          all_pice: parseFloat(res.data.data.price * that.data.num).toFixed(2)
        })
      },
      complete: function () {
        wx.hideLoading();
      }
    })
  },
  creatdayin() {
    let that = this;
    wx.scanCode({
      success(res) {
        let facilityCode = res.result;
        that.setData({
          facilityCode: facilityCode
        })
        wx.showModal({
          title: '设备添加成功',
          icon: 'none'
        })
      }
    })

  },
  // 优惠券
  mycon() {
    wx.navigateTo({
      url: '/modelB/pages/my/my_coupon/my_coupon',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  // 计算价格
  calculateTotal: function () {
    let that = this;
    let all_pice = ((that.data.pice * that.data.num) * 100 - that.data.couponPrice * 100) / 100;
    if (all_pice > 0) {
      that.setData({
        all_pice: parseFloat(all_pice).toFixed(2),
      })
    } else {
      that.setData({
        all_pice: 0
      })
    }

  },
  // 减一
  minusCount: function (e) {
    var num = this.data.num;
    // 商品总数量-1
    if (num > 1) {
      num--;
    }
    // 将数值与状态写回  
    this.setData({
      num: num,
    });
    this.calculateTotal()
    console.log(this.data.num)
    // getCount()
  },
  /* 加数 */
  addCount: function (e) {
    var num = this.data.num;
    // 总数量-1  
    if (num < 1000) {
      num++;
    }
    // 将数值与状态写回  
    this.setData({
      num: num,
    });
    this.calculateTotal()
    // getCount()
  },
  inputPassword(e) {
    let that = this;
    //键盘输入的密码 赋值给inputPassword
    let inputPassword = that.data.inputPassword + e.currentTarget.dataset.key;
    that.setData({
      inputPassword: inputPassword
    })
    console.log(that.data.inputPassword)
    //当输入密码完成时 提交密码并给个提示
    if (that.data.inputPassword.length == 6) {
      wx.showLoading({
        title: '支付中',
      })
      let url = app.globalData.URL + 'facility/printOrder/pay?id=' + that.data.payid + '&payPassword=' + that.data.inputPassword;
      let data = {}
      app.wxRequest('POST', url, data, (res1) => {
        wx.hideLoading();
        if (res1.code == 200) {
          // 支付成功
          that.passwordInputHidden();
          wx.showModal({
            title: '支付成功',
            icon: 'none'
          })
          wx.switchTab({
            url: '/pages/index/index'
          });
        } else if (res1.code == 500) {
          //支付密码错误
          wx.showModal({
            title: "提示",
            content: "余额不足",
            success() {
              that.passwordInputHidden();
            }
          })
          that.setData({
            inputPassword: ''
          });
        } else if (res1.code == 502) {
          //支付密码错误
          wx.showModal({
            title: "提示",
            content: "输入密码错误",
          })
          that.setData({
            inputPassword: ''
          });
        } else if (res1.code == 501) {
          // 未设置支付密码
          wx.showModal({
            title: "提示",
            content: "未设置支付密码",
          })
          that.passwordInputHidden()
          wx.navigateTo({
            url: '/pages/my/setPassword/setPassword'
          })
        }
      }, (err) => {
        console.log(err.errMsg)
      })
    }

  },
  passwordInputHidden() {
    this.setData({
      passwordInputHidden: !this.data.passwordInputHidden //取反 打开关闭小键盘
    });
    this.setData({
      inputPassword: ''
    });
  },
  //删除输入错误的密码
  clear() {
    var index = this.data.inputPassword.length;
    if (index > 0) {
      var inputPassword = this.data.inputPassword.substr(0, index - 1);
      this.setData({
        inputPassword: inputPassword
      });
    }
  },
  mycoupon: function () {
    wx.navigateTo({
      url: '/modelB/pages/my/my_coupon/my_coupon',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  // 预览
  gotophoto: function () {
    let pic = this.data.pics;
    wx.navigateTo({
      url: '/modelA/pages/drawings_printing/picture_editing/picture_editing?pic=' + pic + '&title=' + this.data.title + '&type=' + this.data.type + '&isColours=' + this.data.isColours + '&id=' + this.data.typeCode + '&couponId=' + that.data.couponId,
    })
  },
  submit_tj: function () {
    let that = this;
    console.log(that);
    if (that.data.facilityCode.indexOf("@") == -1) {
      wx.showModal({
        title: '请添加机器',
        icon: 'none',
        success() {
          that.creatdayin();
        }
      })
    } else {
      let content = that.data.content;
      let count = that.data.num;
      let endPage = that.data.choice_end;
      let isColours = that.data.isColours;
      let payType = that.data.payType;
      let printType = that.data.printType;
      let startPage = that.data.choice_star;
      let facilityCode = that.data.facilityCode;
      let title = that.data.title;
      let type = that.data.type;
      let typeCode = that.data.typeCode;
      let url = app.globalData.URL + 'facility/printOrder?content=' + content + '&count=' + count + '&endPage=' + endPage + '&facilityCode=' + facilityCode + '&isColours=' + isColours + '&payType=' + payType + '&printType=' + printType + '&startPage=' + startPage + '&title=' + title + '&type=' + type + '&typeCode=' + typeCode;
      let data = {
        // content:content,
        // count:count,
        // endPage:endPage,
        // facilityCode:facilityCode,
        // isColours:isColours,
        // payType:payType,
        // printType:printType,
        // startPage:startPage,
        // title:title,
        // type:type,
        // typeCode:typeCode
      };
      console.log('url', url);
      console.log('data', data);
      app.wxRequest('POST', url, data, (res) => {
        console.log(res)
        if (res.code != 200) {
          wx.showModal({
            title: res.msg,
            success() {
              wx.switchTab({
                url: '/pages/index/index',
              })
            }
          })
        } else {
          that.setData({
            payid: res.data.id
          })
          if (data.pay && data.print) {
            wx.showModal({
              title: '支付成功',
              icon: 'none',
              success: function () {
                wx.switchTab({
                  url: '/pages/index/index'
                });
              }
            })
          } else if (that.data.payType == 0) {
            that.passwordInputHidden();
          } else if (that.data.payType == 1) {
            let wxzf = res.data;
            console.log(wxzf)
            wx.requestPayment({
              timeStamp: wxzf.timeStamp,
              nonceStr: wxzf.nonceStr,
              package: wxzf.package,
              signType: wxzf.signType,
              paySign: wxzf.paySign,
              success(res) {
                console.log('支付成功')
              },
              fail(res) { }
            })
          }
          console.log(res)
        }


      }, (err) => {
        console.log(err.errMsg)
      })
    }
  }
})
